//
//  RequestAPIFunction.m
//  Bespoke
//
//  Created by Cheng on 12/9/10.
//
//

#import "RequestAPIFunction.h"
#import "Split.h"

@implementation RequestAPIFunction

#pragma mark - Upload Camera or Photo

+ (NSData *)XML_PostRequest:(NSString *)xmlMessage urlString:(NSString *)urlString ImageData:(NSData *)SetImage {
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    NSMutableData *body = [NSMutableData data];
    
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:SetImage]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"xml\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:xmlMessage] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];
    
    NSError *ErrMSG = [NSError alloc];
    //get response
    NSHTTPURLResponse* urlResponse = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&ErrMSG];

    //NSLog(@"Result:%@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
    NSLog(@"Response Code: %d", [urlResponse statusCode]);
    NSLog(@"Error Code %d",[ErrMSG code]);
    
    UIAlertView *alertView;
    
    if ([ErrMSG code]==-1009) {
        
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"No network connection." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        return nil;
    }
    else if ([ErrMSG code]==-1001) {
        
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Connection is time out." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        return nil;
    }
    
    return responseData;
}

#pragma mark - Request for NSData

+ (NSData *)XML_PostRequest:(NSString *)xmlMessage urlString:(NSString *)urlString {
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[xmlMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"XML::::::%@",xmlMessage);
    [request setHTTPBody:postBody];
    
    NSError *ErrMSG = [NSError alloc];
    //get response
    NSHTTPURLResponse* urlResponse = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&ErrMSG];
    
    NSLog(@"Result:%@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
    NSLog(@"Response Code: %d", [urlResponse statusCode]);
    NSLog(@"Error Code %d",[ErrMSG code]);
    
    UIAlertView *alertView;
    
    if ([ErrMSG code]==-1009) {
        
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"No network connection." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        return nil;
    }
    else if ([ErrMSG code]==-1001) {
        
        alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Connection is time out." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        return nil;
    }
    
    return responseData;
}

@end
